package com.study.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ：ihao ∶）
 * @date ：Created in 2023/10/20 11:07
 */
@Slf4j
public class SmsUtil {

    public static String getShortLink(String url) {
        JSONObject json = new JSONObject();
        json.put("apikey", "7138889ab977710dcf38ed2c2c97654f");
        json.put("long_url", url);
        String resultStr = HttpUtil.post("https://sms.yunpian.com/v2/short_url/shorten.json", json);
        JSONObject result = JSONObject.parseObject(resultStr);
        if (result.getInteger("code") == 0) {
            return result.getJSONObject("short_url").getString("short_url");
        } else {
            log.error("生成短链接失败 result={}", result);
        }
        return null;
    }

    public static void main(String[] args) {
        String shortLink = getShortLink("https://www.baidu.com");
        System.out.println(shortLink);
    }
}
